package main

import (
	"fmt"
	"reflect"
	"time"
)

/*
定时器
 */


func test1(){
	//ticker 使用
	ticker := time.NewTicker(time.Second *1)
	ch := make(chan int)
	go func(){
			var x int
			for x < 10{
				select {
				case <-ticker.C:
					x++
					fmt.Printf("%d\n",x)
				}
			}
			ticker.Stop()
			ch<-0
	}()
	<-ch
}

func test2(){
	//Timer
	timer := time.NewTimer(time.Second *1)
	ch := make(chan int)
	go func(){
		var x int
		for {
			select {
			case <-timer.C:
				x++
				fmt.Printf("%d,%s \n",x, time.Now().Format("2006-01-02 15:04:05"))
				if x < 10 {
					timer.Reset(time.Second * 1)
				}else{
					ch <- x
				}
			}
		}
	}()
	<-ch
}


type jobFunc2 func(j *job)
type job struct{
	jf jobFunc2
	params map[string]interface{}
	ch chan int
}
func NewJob() *job{
	return &job{
		params: make(map[string]interface{}),
		ch:make(chan int),
	}
}

func (j *job) Run(t time.Duration){
	ticker := time.NewTicker(time.Second *t)
	go func(){
		for {
			select {
			case <-ticker.C:
				j.jf(j)
			case <-j.ch:
				fmt.Println("run over")
				ticker.Stop()
				break
			}
		}
	}()
}


func main()  {
	//test1()
	//test2()

	//j := NewJob()
	//j.jf = func(jj *job){
	//	fmt.Println("run timer...", time.Now().Format("15:04:04 2006/02/01"), jj.params)
	//}
	//j.params["p1"] = "p1---------"
	//j.params["p2"] = "p2--------------"
	//
	//j.Run(1)
	//tt := time.NewTimer(3*time.Second)
	//<-tt.C
	//j.ch <- 0
	//{
	//	tt := time.NewTimer(10 * time.Second)
	//	<-tt.C
	//}
	//fmt.Println("over-")

	type ReconnectRummyRoundNode struct {
		RoomId           int32    `protobuf:"varint,1,opt,name=roomId" json:"roomId,omitempty"`
		GameUrl          *string   `protobuf:"bytes,2,opt,name=gameUrl" json:"gameUrl,omitempty"`
		GameToken        *string   `protobuf:"bytes,3,opt,name=gameToken" json:"gameToken,omitempty"`
		LevelId          *int32    `protobuf:"varint,4,opt,name=levelId" json:"levelId,omitempty"`
		//Currency         *Currency `protobuf:"varint,5,opt,name=currency,enum=protobuf.Currency" json:"currency,omitempty"`
		//XXX_unrecognized []byte    `json:"-"`
	}
	roomId := int32(111)
	gameUrl := "xxx/xx"
	gameToken := "token"
	levelId := int32(11)
	p := ReconnectRummyRoundNode{roomId, &gameUrl, &gameToken, &levelId}
	v := reflect.ValueOf(p)
	count := v.NumField()
	for i := 0; i < count; i++ {
		f := v.Field(i)
		tagVal := v.Type().Field(i).Tag.Get("protobuf")
		fmt.Println("__", tagVal, f.Type().Kind())
		if f.Type().Kind().String() == "ptr" {
			fmt.Println(v.Type().Field(i).Name, f.Elem())
		}else {
			fmt.Println(v.Type().Field(i).Name, f)
		}

		//switch f.Type().String() {
		//case "*string":
		//	fmt.Println(f.Elem())
		//case "*int":
		//	fmt.Println(f.Int())
		//case "*bool":
		//	fmt.Println(f.Bool())
		//}
	}
	fmt.Println(v.NumMethod())
	//iter := v.MapRange()
	//for iter.Next() {
	//	k := iter.Key()
	//	v := iter.Value()
	//	fmt.Println(k, v)
	//}

}

